package cn.lzm.server.tcp;

import cn.lzm.server.Server;
import io.vertx.core.Vertx;
import io.vertx.core.net.NetServer;

/**
 * @author lzm
 * @Date 2024/11/5
 * @description 基于tcp协议的服务器，解决半包和粘包问题
 */
public class VertxTcpServerV5 implements Server {

    @Override
    public void start(int port) {
        Vertx vertx = Vertx.vertx();
        NetServer server = vertx.createNetServer();

        // 处理请求
        server.connectHandler(new TcpServerHandler());

        // 启动TCP服务器并监听指定端口
        server.listen(port, result -> {
            if (result.succeeded()) {
                System.out.println("Tcp server started on port " + port);
            } else {
                System.out.println("Failed to start Tcp server: " + result.cause());
            }
        });
    }

    public static void main(String[] args) {
        new VertxTcpServerV5().start(8408);
    }

}
